﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using util;

namespace WebApplication1.login
{
    public partial class login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            String sql = "select * from user where username = '"+ this.usernmae.Text+ "' and password = '" + this.password.Text + "';";          
            DataTable table = MySqlHelper.GetDataTable(sql);
            //2.获取第一行数据
            if (table.Rows.Count == 0)
            {
                Response.Write("<script>alert('账号或密码不正确');</script>");
            }
            else {
                String username = table.Rows[0]["username"].ToString();
                String name = "";
                Application["username"] = username;
                Application["userId"] = table.Rows[0]["id"];
                int roleId = Int32.Parse(table.Rows[0]["role_id"].ToString());
                sql = "select * from role  where id= " + roleId + ";";
                DataTable role = MySqlHelper.GetDataTable(sql);
                String roleName = role.Rows[0]["name"].ToString();
                Application["rolename"] = roleName;
                if (roleName.Equals("系统管理员")) {
                    Server.Transfer("\\sysAdministrator\\adminIndex.aspx");
                } else if (roleName.Equals("宿舍管理员")) {
                     Server.Transfer("\\dormitoryAdministrator\\Annouce.aspx");
                }
                else if (roleName.Equals("学生"))
                {
                    String sql1 = "select * from student where user_id = '" + table.Rows[0]["id"]+"';";
                    DataTable table1 = MySqlHelper.GetDataTable(sql1);
                    name = table1.Rows[0]["name"].ToString();
                    Application["name"] = name;
                    Server.Transfer("\\studentUser\\index.aspx");
                }
                else if (roleName.Equals("学生会工作者"))
                {
                    Server.Transfer("\\studentUnion\\WebForm2.aspx");
                }

            }
            this.DataBind();
        }
    }
}